#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>

#define PROCNUM 	10
#define FINEANME	"./out"
#define LINESIZE	1024

static void process_lockf(void)
{
	FILE *fp;
	int fd;

	char linebuf[LINESIZE];

	fp = fopen(FINEANME, "r+");
	if(fp == NULL){
		perror("fopen()");
		exit(1);
	}

	fd = fileno(fp);

	lockf(fd, F_LOCK, 0);
	fgets(linebuf, LINESIZE, fp);
	fseek(fp, 0, SEEK_SET);
	fprintf(fp, "%d\n", atoi(linebuf) + 1);
	fflush(fp);
	lockf(fd, F_LOCK, 0);
	fclose(fp);

}
/*
 * 文件锁
 * */
int main(int argc, char **argv)
{

	int i, pid;

	for (i = 0; i < PROCNUM; i++) {
		pid = fork();
		if(pid < 0){
			perror("fork()");
			exit(1);
		}
		if(pid == 0){
			process_lockf();
			exit(0);
		}
	}

	for (i = 0; i < PROCNUM; i++) {
		wait(NULL);
	}

	exit(0);
}
